//
//  SongsCollectionViewCell.m
//  LuckyRadio
//
//  Created by Mr、J on 15/11/16.
//  Copyright © 2015年 chenjun. All rights reserved.
//

#import "SongsCollectionViewCell.h"
#import <UIImageView+WebCache.h>


#define SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width

#define SCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height

#define WIDTH_PRO(X) (X)*(SCREEN_WIDTH/375)

#define HEIGHT_PRO(Y) (Y)*(SCREEN_HEIGHT/667)

@implementation SongsCollectionViewCell

- (void)dealloc
{
    [_songListModel release];
    [_image release];
    [_titleLabel release];
    [_listenerLabel release];
    [_listenImageView release];
    [super dealloc];
}

-(instancetype)initWithFrame:(CGRect)frame
{
    if ([super initWithFrame:frame]) {
        [self createSubViews];
    }
    return self;
}

-(void)createSubViews
{
    
    self.image = [[UIImageView alloc] init];
    [self.contentView addSubview:self.image];
    [_image release];
    
    self.titleLabel = [UILabel new];
    self.titleLabel.font = [UIFont systemFontOfSize:15];
    [self.contentView addSubview:self.titleLabel];
    self.titleLabel.numberOfLines = 0;
    [_titleLabel release];
    
    self.listenImageView = [UIImageView new];
    [self.image addSubview:self.listenImageView];
    self.listenImageView.backgroundColor = [UIColor clearColor];
    [_listenImageView release];
    
    self.listenerLabel = [UILabel new];
    self.listenerLabel.font = [UIFont systemFontOfSize:12];
    self.listenerLabel.textColor = [UIColor whiteColor];
    self.listenerLabel.shadowColor = [UIColor blackColor];
    self.listenerLabel.textAlignment = NSTextAlignmentRight;
    [self.image addSubview:self.listenerLabel];
    [_listenerLabel release];
    
}

-(void)layoutSubviews
{
    [super layoutSubviews];
    self.image.frame = CGRectMake(0, 0, (SCREEN_WIDTH - WIDTH_PRO(40)) / 3, (SCREEN_WIDTH - WIDTH_PRO(40)) / 3);
    self.titleLabel.frame = CGRectMake(0, self.image.frame.size.height + 10, (SCREEN_WIDTH - WIDTH_PRO(40)) / 3, HEIGHT_PRO(40));
    
    CGRect listenerRect = [self.listenerLabel.text boundingRectWithSize:CGSizeMake(320, 1000) options:NSStringDrawingUsesLineFragmentOrigin attributes:[NSDictionary dictionaryWithObjectsAndKeys:self.listenerLabel.font,NSFontAttributeName, nil] context:nil];
    self.listenerLabel.frame = CGRectMake(self.image.frame.size.width - listenerRect.size.width - WIDTH_PRO(10), HEIGHT_PRO(5), listenerRect.size.width, HEIGHT_PRO(20));
    
    self.listenImageView.frame = CGRectMake(self.image.frame.size.width - self.listenerLabel.frame.size.width - WIDTH_PRO(25), HEIGHT_PRO(9), WIDTH_PRO(12), HEIGHT_PRO(12));
}

- (void)setSongListModel:(SongListModel *)songListModel
{
    if (_songListModel != songListModel) {
        [_songListModel release];
        _songListModel = [songListModel retain];
    }
    
    [self.image sd_setImageWithURL:[NSURL URLWithString:songListModel.picUrl] placeholderImage:[UIImage imageNamed:@"Read_zhanwei"]];
    self.titleLabel.text = songListModel.name;
    self.listenImageView.image = [UIImage imageNamed:@"iconfont-headphone"];
    
     self.listenerLabel.text = [NSString stringWithFormat:@"%@", songListModel.listenCount];
    
    if ([songListModel.listenCount floatValue] > 10000) {
        CGFloat count = [songListModel.listenCount floatValue] / 10000;
        self.listenerLabel.text = [NSString stringWithFormat:@"%.1f万", count];
    }
    
   
    
}


@end
